package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/4/3 19:55
 */
public class J04 {


    /** https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/ */
    public static boolean findNumberIn2DArray(int[][] matrix, int target) {
        if(matrix.length == 0 || matrix[0].length == 0) {
            return false;
        }
        int rows = matrix.length;
        int r = 0, c = matrix[0].length-1;
        while (r < rows && c >= 0) {
            if(matrix[r][c] == target) {
                return true;
            }else if(matrix[r][c] < target) {
                r++;
            }else {
                c--;
            }
        }
        return false;
    }

    // for test
    public static void main(String[] args) {
        int[][] matrix = {{1,4,7,11,15},{2,5,8,12,19},{3,6,9,16,22},{10,13,14,17,24},{18,21,23,26,30}};
        int target = 5;
        System.out.println(findNumberIn2DArray(matrix, target));
    }

}
